package com.lpit.service.impl;

import com.lpit.constant.Constant;
import com.lpit.dto.UserAndTableTheBehaviorDto;
import com.lpit.mapper.BrowsingHistoryMapper;
import com.lpit.result.ResponseResult;
import com.lpit.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * 用户对文章的收藏行为
 *
 * @author Administrator
 */
@Service
public class CollectServiceImpl implements CollectService {

    @Autowired
    private BrowsingHistoryMapper browsingHistoryMapper;
    /**
     * 用户对文章收藏
     * @param userAndTableTheBehaviorDto
     * @return ResponseResult<Integer>
     */
    @Override
    public ResponseResult<Integer> collect(UserAndTableTheBehaviorDto userAndTableTheBehaviorDto) {
        /* 是否存在用户的id */
        if (null == userAndTableTheBehaviorDto.getUserId() || 0 == userAndTableTheBehaviorDto.getUserId()) {
            return new ResponseResult<>(404, null, Constant.THE_USER_WAS_NOT_FOUND);
        }
        /* 是否存在文章id*/
        if (null == userAndTableTheBehaviorDto.getArticleId() || 0 == userAndTableTheBehaviorDto.getArticleId()) {
            return new ResponseResult<>(404, null, Constant.NO_RELATED_ARTICLE_FOUND);
        }
        /* 是否存在行为值 , 行为值是否违法*/
        if (null == userAndTableTheBehaviorDto.getArticleStatus()) {
            userAndTableTheBehaviorDto.setArticleStatus(Constant.SC);
        }
        /* 是否存在时间信息*/
        if (null == userAndTableTheBehaviorDto.getDateTime()) {
            /* 创建当前的系统时间*/
            userAndTableTheBehaviorDto.setDateTime(new SimpleDateFormat(Constant.TIME_FORMAT).format(new Date()));
        }
        Integer integer = browsingHistoryMapper.addCollect(userAndTableTheBehaviorDto);
        if (null == integer || 0 == integer) {
            return new ResponseResult<>(404, null, Constant.MISTAKE);
        }
        return new ResponseResult<>(200, integer, Constant.SUCCESS);
    }
}
